﻿namespace HelperLibrary;

/// <summary>
/// 日期工具类
/// </summary>
public static class DateTimeHelper
{
    /// <summary>
    /// yyyy-MM
    /// </summary>
    /// <returns></returns>
    public static string ToYM(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM");
    }

    /// <summary>
    /// MM-dd
    /// </summary>
    /// <returns></returns>
    public static string ToMD(this DateTime dateTime)
    {
        return dateTime.ToString("MM-dd");
    }
    /// <summary>
    /// yyyy-MM-dd
    /// </summary>
    /// <returns></returns>
    public static string ToYMD(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd");
    }

    /// <summary>
    /// yyyy-MM-dd HH:mm:ss
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string ToYMDHMS(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd HH:mm:ss");
    }

    /// <summary>
    /// yyyyMMddHHmmss
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string ToNum(this DateTime dateTime)
    {
        return dateTime.ToString("yyyyMMddHHmmss");
    }

    /// <summary>
    /// yyyyMMddHHmmssfff
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string ToOrderNum(this DateTime dateTime)
    {
        return dateTime.ToString("yyyyMMddHHmmssfff");
    }
    /// <summary>
    /// yyyy-MM-dd HH:mm
    /// </summary>
    /// <param name="dateTime"></param>
    /// <returns></returns>
    public static string ToYMDHM(this DateTime dateTime)
    {
        return dateTime.ToString("yyyy-MM-dd HH:mm");
    }

    /// <summary>
    /// HH:mm
    /// </summary>
    /// <returns></returns>
    public static string ToHHmm(this DateTime dateTime)
    {
        return dateTime.ToString("HH:mm");
    }

    /// <summary>
    /// HH:mm:ss
    /// </summary>
    /// <returns></returns>
    public static string ToHHmmss(this DateTime dateTime)
    {
        return dateTime.ToString("HH:mm:ss");
    }
    /// <summary>
    /// 时间转换为时间戳
    /// </summary>
    /// <param name="date"></param>
    /// <returns></returns>
    public static long ToUnixTime(this DateTime date)
    {
        if (date == DateTime.MinValue)
        {
            return 0;
        }
        DateTime startTime = new DateTime(1970, 1, 1).ToLocalTime();
        long timeStamp = (long)(date - startTime).TotalSeconds; // 相差秒数
        return timeStamp;
    }

    /// <summary>
    /// 求两个日期间隔时间
    /// </summary>
    /// <param name="dateTime1"></param>
    /// <param name="dateTime2"></param>
    /// <returns></returns>
    public static double DateDiff(DateTime dateTime1, DateTime dateTime2, TimeType timeType = TimeType.Minute)
    {
        TimeSpan ts1 = new TimeSpan(dateTime1.Ticks);
        TimeSpan ts2 = new TimeSpan(dateTime2.Ticks);
        TimeSpan ts = ts1.Subtract(ts2).Duration();

        double result = 0;
        switch (timeType)
        {
            case TimeType.Day:
                result = ts.TotalDays;
                break;
            case TimeType.Hour:
                result = ts.TotalHours;
                break;
            case TimeType.Minute:
                result = ts.TotalMinutes;
                break;
            case TimeType.Second:
                result = ts.TotalSeconds;
                break;
            default:
                result = ts.TotalMinutes;
                break;
        }

        return result;
    }

    /// <summary>
    /// 分钟转换为小时分钟字符串
    /// </summary>
    /// <param name="minutes"></param>
    /// <returns></returns>
    public static string MinutesToHourStr(int minutes)
    {
        return $"{minutes / 60}小时{minutes % 60}分钟";
    }

    /// <summary>
    /// 获取中文星期
    /// </summary>
    /// <param name="ENDayOfWeek"></param>
    /// <returns></returns>
    public static string GetCNDayOfWeek(string ENDayOfWeek)
    {
        switch (ENDayOfWeek)
        {
            case "Monday":
                return "星期一";
            case "Tuesday":
                return "星期二";
            case "Wednesday":
                return "星期三";
            case "Thursday":
                return "星期四";
            case "Friday":
                return "星期五";
            case "Saturday":
                return "星期六";
            case "Sunday":
                return "星期日";
            default:
                throw new Exception("Invalid ENDayOfWeek");
        }
    }

    /// <summary>
    /// 默认星期数组
    /// </summary>
    /// <returns></returns>
    public static List<int> DefaultWeek()
    {
        return new List<int> { 1, 2, 3, 4, 5, 6, 7 };
    }
    /// <summary>
    /// 获取中文星期
    /// </summary>
    /// <param name="ENDayOfWeek"></param>
    /// <returns></returns>
    public static string GetNumOfWeek(int ENDayOfWeek)
    {
        switch (ENDayOfWeek)
        {
            case 1:
                return "星期一";
            case 2:
                return "星期二";
            case 3:
                return "星期三";
            case 4:
                return "星期四";
            case 5:
                return "星期五";
            case 6:
                return "星期六";
            case 0:
                return "星期日";
            default:
                throw new Exception("Invalid ENDayOfWeek");
        }
    }

    /// <summary>
    /// 获取数字星期
    /// </summary>
    /// <param name="ENDayOfWeek"></param>
    /// <returns></returns>
    public static int GetNumOfWeek(string ENDayOfWeek)
    {
        switch (ENDayOfWeek)
        {
            case "Monday":
                return 1;
            case "Tuesday":
                return 2;
            case "Wednesday":
                return 3;
            case "Thursday":
                return 4;
            case "Friday":
                return 5;
            case "Saturday":
                return 6;
            case "Sunday":
                return 7;
            default:
                throw new Exception("Invalid ENDayOfWeek");
        }
    }

    /// <summary>
    /// 获取日期所在周的某一天的日期
    /// </summary>
    /// <param name="date"></param>
    /// <param name="weekDay"></param>
    /// <returns></returns>
    public static DateTime GetWeekDay(DateTime date, int weekDay)
    {
        if (date.DayOfWeek == 0)
        {
            date = date.AddDays(-1);
        }
        return date.AddDays((-(int)date.DayOfWeek) + weekDay);
    }

    public static List<List<DateTime>> GetWeeks(string stime, string etime)
    {
        try
        {
            List<List<DateTime>> weeks = new List<List<DateTime>>();
            DateTime st = Convert.ToDateTime(stime);
            DateTime et = Convert.ToDateTime(etime);

            List<DateTime> week = new List<DateTime>();
            while (DateTime.Compare(st, et) <= 0)
            {
                if (st.DayOfWeek == DayOfWeek.Monday)
                {
                    if (week.Count > 0)
                        weeks.Add(week);
                    week = new List<DateTime>();
                }
                week.Add(st);
                st = st.AddDays(1);
            }
            if (week.Count > 0)
                weeks.Add(week);
            return weeks;
        }
        catch (Exception ex)
        {
            throw new Exception("获取学期周数组失败");
        }
    }
}

/// <summary>
/// 时间类型枚举
/// </summary>
public enum TimeType
{
    /// <summary>
    /// 天
    /// </summary>
    Day = 3,
    /// <summary>
    /// 小时
    /// </summary>
    Hour = 4,
    /// <summary>
    /// 分钟
    /// </summary>
    Minute = 5,
    /// <summary>
    /// 秒
    /// </summary>
    Second = 6
}

/// <summary>
/// 日期类型
/// </summary>
public enum DateTimeType
{
    /// <summary>
    /// 近七天
    /// </summary>
    Latest7Days = 1,
    /// <summary>
    /// 近一个月
    /// </summary>
    Latest1Month = 2,
    /// <summary>
    /// 本学期
    /// </summary>
    LarestTerm = 3,
    /// <summary>
    /// 时间段
    /// </summary>
    CustomTime = 4,
}

/// <summary>
/// 统计日期类型
/// </summary>
public enum StatisticsDateTypeEnum
{
    /// <summary>
    /// 今日
    /// </summary>
    Today = 0,
    /// <summary>
    /// 昨日
    /// </summary>
    YesterDay = 1,
    /// <summary>
    /// 本周
    /// </summary>
    ThisWeek = 2,
    /// <summary>
    /// 上周
    /// </summary>
    LastWeek = 3,
    /// <summary>
    /// 本月
    /// </summary>
    ThisMonth = 4,
    /// <summary>
    /// 上月
    /// </summary>
    LastMonth = 5,
    /// <summary>
    /// 本学期
    /// </summary>
    LarestTerm = 6,
    /// <summary>
    /// 自定义
    /// </summary>
    Custom = 7,
}
